<?php
class SP_Database_Field
{
	private $model_name;
	private $base_name;
	private $validator;
	private $relation;
	private $value;
	private $return_type='string';
	private $default_value;
	private $not_null=true;
	private $need_validate=false;
	private $was_modified=false;
	private $validate_errors=array();
	
	public function __construct($attr=null)
	{
		if(is_array($attr))
		{
			foreach($attr as $key => $value)
			{
				$this->$key=$value;
			}
			if(!isset($attr['base_name']) && isset($attr['model_name']))
			{
				$array=SP_Common_Functions::splitCamelCase($attr['model_name']);
				$this->base_name=SP_Common_Functions::arrayToString($array, $glue='_');
			}
		}
	}
	
	public function setModelName($value)
	{
		$this->model_name=$value;
		$array=SP_Common_Functions::splitCamelCase($value);
		$this->base_name=SP_Common_Functions::arrayToString($array, $glue='_');
		return $this;
	}
	
	public function setBaseName($value)
	{
		$this->base_name=$value;
		return $this;
	}
	
	public function setValidator($value)
	{
		$this->validator=$value;
		$this->need_validate=true;
		return $this;
	}
	
	public function setRelation(SP_Database_Relation $value)
	{
		$this->relation=$value;
		return $this;
	}
	
	public function unsetValue()
	{
		$this->value=null;
	}
	
	public function validate($value=null)
	{
		if( empty($value) ) $value=$this->value;
		if($this->need_validate)
		{
			if(!$this->validator) throw new exception("Validator required for ".$this->model_name." field");
			$this->validator->setInput($value);
			if ( !$this->validator->validate() )
			{
				$this->validate_errors=$this->validator->getResultErrors();
				return false;
			}
		}
		return true;
	}
	
	public function setValue($value)
	{
		if( $this->validate($value) )
		{
			$this->value=$value;
			$this->was_modified=true;
			return true;
		}
		return false;
	}
	
	public function setValueForce($value)
	{
		$this->value=$value;
	}
	
	public function addValue($value,$key=null)
	{
		if( empty($key) )
			$this->value[]=$value;
		else
			$this->value[$key]=$value;
		$this->was_modified=true;
	}
	
	public function setReturnType($value)
	{
		$this->return_type=strtolower($value);
		return $this;
	}
	
	public function setDefault($value)
	{
		$this->default_value=$value;
		return $this;
	}
	
	public function setNotNull($value)
	{
		$this->not_null=(bool)$value;
		return $this;
	}
	
	public function setNeedValidate($value)
	{
		$this->need_validate=(bool)$value;
		return $this;
	}
	
	public function setWasModified($value)
	{
		$this->was_modified=(bool)$value;
		return $this;
	}
	
	public function getModelName()
	{
		return $this->model_name; 
	}
	
	public function getBaseName()
	{
		return $this->base_name;
	}
	
	public function getValidator()
	{
		return $this->validator;
	}
	
	public function hasRelation()
	{
		return isset($this->relation);
	}
	
	public function getRelation()
	{
		return $this->relation;
	}
	
	public function getValue()
	{
		if( $this->hasRelation() )
			return $this->value;
		else
			return $this->getTypedValue();
	}
	
	private function getTypedValue()
	{
		if( empty($this->value) ) return null;
		switch ($this->return_type) 
		{
			case 'string':
				return (string)$this->value;
			case 'float':
				return (float)$this->value;
			case 'integer':
				return (integer)$this->value;
			case 'boolean' :
				return (boolean)$this->value;
		}
	}
	
	public function getDefault()
	{
		return $this->default_value;
	}
	
	public function getNotNull()
	{
		return $this->not_null;
	}
	
	public function getWasModified()
	{
		return $this->was_modified;
	}
	
	public function getValidateErrors()
	{
		return $this->validate_errors;
	}
	
}